﻿$(document).ready(function () {
    pageload();
    $("#btnshow_tpl").click(takepills.showadd);
});
var methods = new GetDataJSON();
function pageload() {
    birthday.init();
    holiday.init();
}
var takepills = {
    showadd: function () { layerShow('takepills'); },
    showedit: function () { layerShow('takepills'); },
    showdelete: function () { layerShow('takepillsdelete'); },
    save: function () {
        var model = this.getmodel();
        console.log(model);
    },
    del: function () { },
    getmodel: function () {
        var model = new Object();
        model.id = $("#takepills_id").val();
        model.hours = $("#takepills_hours").val();
        model.minute = $("#takepills_minute").val();
        return model;
    }
}

var birthday = {
    birthdayList: null,
    birthdayModel: null,
    birthdayDeleteModel: null,
    init: function () {
        this.birthdayList = new Vue({ el: '#birthday_List', data: { items: null } });
        this.birthdayModel = new Vue({ el: '#birthday', data: { model: null } });
        this.birthdayDeleteModel = new Vue({ el: '#birthdaydelete', data: { model: null } });
        this.listload();
    },
    listload: function () {
        function callback(result) {
            if (!checkStatus(result.Status)) {
                $("#birthday_List_Error").show().html("<span class='column3'>" + result.Message + "</span>");
            } else {
                $("#birthday_List_Error").hide();
            }
            birthday.birthdayList.items = result.Data;
        }
        methods.BirthdayMethods.SelectBirthday(callback);
    },
    showadd: function () {
        this.birthdayModel.model = null;
        layerShow('birthday');
    },
    showedit: function (_id) {
        var model = this.getcurrentModel(_id);
        this.birthdayModel.model = model;
        layerShow('birthday');
    },
    showdelete: function (_id) {
        var model = this.getcurrentModel(_id);
        this.birthdayDeleteModel.model = model;
        layerShow('birthdaydelete');
    },
    save: function () {
        var model = this.birthdayModel.model;
        if (model == null) {
            alert("请填写信息");
            return;
        }
        function callback(result) {
            if (!checkStatus(result.Status)) {
                alert(result.Message);
            } else {
                birthday.listload();
                layer.closeAll();
            }
        }
        if (model == null || model.id == null || model.id == undefined) {
            methods.BirthdayMethods.AddBirthday(model, callback);
        } else {
            methods.BirthdayMethods.ChangeBirthday(model, callback);
        }
    },
    del: function () {
        var model = this.birthdayDeleteModel.model;
        function callback(result) {
            if (!checkStatus(result.Status)) {
                alert(result.Message);
            } else {
                birthday.listload();
                layer.closeAll();
            }
        }
        methods.BirthdayMethods.DelHoliBirthday(model, callback);
    },
    getcurrentModel: function (_id) {
        var model = new Object();
        var thislist = new Object();
        var list = this.birthdayList.items;
        for (var i = 0; i < list.length; i++) {
            if (list[i][0] == _id) {
                thislist = list[i];
                break;
            }
        }
        model.id = thislist[0];
        model.title = thislist[1];
        model.starttime = thislist[2];
        model.contt = thislist[3];
        model.onoff = thislist[4];
        return model;
    }
}

var holiday = {
    holidayList: null,
    holidayModel: null,
    holidayDeleteModel: null,
    init: function () {
        this.holidayList = new Vue({ el: '#holiday_List', data: { items: null } });
        this.holidayModel = new Vue({ el: '#holiday', data: { model: null } });
        this.holidayDeleteModel = new Vue({ el: '#holidaydelete', data: { model: null } });
        this.listload();
    },
    listload: function () {
        function callback(result) {
            if (!checkStatus(result.Status)) {
                $("#holiday_List_Error").show().html("<span class='column3'>" + result.Message + "</span>");
            } else {
                $("#holiday_List_Error").hide();
            }
            holiday.holidayList.items = result.Data;
        }
        methods.HolidayMethods.SelectHoliday(callback);
    },
    showadd: function () {
        this.holidayModel.model = null;
        layerShow('holiday');
    },
    showedit: function (_id) {
        var model = this.getcurrentModel(_id);
        this.holidayModel.model = model;
        layerShow('holiday');
    },
    showdelete: function (_id) {
        var model = this.getcurrentModel(_id);
        this.holidayDeleteModel.model = model;
        layerShow('holidaydelete');
    },
    save: function () {
        var model = this.holidayModel.model;
        if (model == null) {
            alert("请填写信息");
            return;
        }
        function callback(result) {
            if (!checkStatus(result.Status)) {
                alert(result.Message);
            } else {
                holiday.listload();
                layer.closeAll();
            }
        }
        if (model == null || model.id == null || model.id == undefined) {
            methods.HolidayMethods.AddHoliday(model, callback);
        } else {
            methods.HolidayMethods.ChangeHoliday(model, callback);
        }
    },
    del: function () {
        var model = this.holidayDeleteModel.model;
        function callback(result) {
            if (!checkStatus(result.Status)) {
                alert(result.Message);
            } else {
                holiday.listload();
                layer.closeAll();
            }
        }
        methods.HolidayMethods.DelHoliBirthday(model, callback);
    },
    getcurrentModel: function (_id) {
        var model = new Object();
        var thislist = new Object();
        var list = this.holidayList.items;
        for (var i = 0; i < list.length; i++) {
            if (list[i][0] == _id) {
                thislist = list[i];
                break;
            }
        }
        model.id = thislist[0];
        model.title = thislist[1];
        model.starttime = thislist[2];
        model.contt = thislist[3];
        model.onoff = thislist[4];
        return model;
    }
}
$(".switchopenbtn").click(function () {
    var checked = $(this).find("input[type=checkbox]").attr("checked");
    if (checked) {
        $(this).prev().html("关");
        $(this).find("input[type=checkbox]").attr("checked", false);
        $(this).find("span").css({ left: 0 });
    } else {
        $(this).prev().html("开");
        $(this).find("input[type=checkbox]").attr("checked", true);
        $(this).find("span").css({ left: 37 });
    }
});